package cn.jdy.plugins.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.Advertising;
import cn.jdy.plugins.entity.Chat;
import cn.jdy.plugins.entity.CheckLive;

/***
 * @desc   审核实现类
 * @author jga
 * @since  2017-9-23
 */
@Service
public class CheckLiveService extends BaseDao<CheckLive>{
	/**
	 * 按条件查询
	 * @param TeachAdvice
	 * @return
	 */
	public List<CheckLive> query(CheckLive obj, String... date) {
		if(null == obj) return super.findAll();
		if(obj.getiDisplayLength() > 0){
		  return super.find(createFilter(obj, date),obj.getiDisplayStart(),obj.getiDisplayLength());
		}
		return super.find(createFilter(obj, date));
	}

	public Long count(CheckLive obj, String... date) {
		return super.count(createFilter(obj, date));
	}
	
	/**
	 * 拼装条件
	 * @param evaluationProject
	 * @param date
	 * @return
	 */
	private DetachedCriteria createFilter(CheckLive obj, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  
	
	  
	
	  if(!StringUtil.isNullOrEmpty(obj.getName())){
		  dc.add(Restrictions.like("name", obj.getName().trim(), MatchMode.ANYWHERE));
	  }
	  if(!StringUtil.isNullOrEmpty(obj.getLiveId())){
		  dc.add(Restrictions.eq("liveId", obj.getLiveId()));
	  }
	  if(!StringUtil.isNullOrEmpty(obj.getUserNickName())){
		  dc.add(Restrictions.like("userNickName", obj.getUserNickName().trim(), MatchMode.ANYWHERE));
	  }
	  if(!StringUtil.isNullOrEmpty(obj.getSendStatus())){
		  dc.add(Restrictions.like("sendStatus", obj.getSendStatus().trim(), MatchMode.ANYWHERE));
	  }
	  
	  
	  //dc.addOrder(Order.desc("insertTime"));
		return dc;
	}
	
	public List<CheckLive> queryids(Object[] data,CheckLive chat,String... date) {
		if(null == chat) return super.findAll();
		if(chat.getiDisplayLength() > 0){
		  return super.find(createFilterids(data,chat, date),chat.getiDisplayStart(),chat.getiDisplayLength());
		}
		return super.find(createFilterids(data,chat, date));
	}

	public Long countids(Object[] chat,CheckLive obj, String... date) {
		return super.count(createFilterids(chat,obj, date));
	}
	//跟多个直播id去查询
	private DetachedCriteria createFilterids(Object[]  data,CheckLive obj, String... date)  {
		  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
			  dc.add(Restrictions.in("liveId", data));
			
			  if(!StringUtil.isNullOrEmpty(obj.getName())){
				  dc.add(Restrictions.like("name", obj.getName().trim(), MatchMode.ANYWHERE));
			  }
			
			  if(!StringUtil.isNullOrEmpty(obj.getUserNickName())){
				  dc.add(Restrictions.like("userNickName", obj.getUserNickName().trim(), MatchMode.ANYWHERE));
			  }
			  if(!StringUtil.isNullOrEmpty(obj.getSendStatus())){
				  dc.add(Restrictions.like("sendStatus", obj.getSendStatus().trim(), MatchMode.ANYWHERE));
			  }
			  
		 dc.addOrder(Order.desc("createTime"));
		  return dc;
		}

}
